package com.dingtalk.h5app.quickstart;

import cn.afterturn.easypoi.word.entity.bookmark.ListWordEntityVO;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.h5app.quickstart.config.RestTemplateConfig;
import com.dingtalk.h5app.quickstart.domain.Result;
import com.dingtalk.h5app.quickstart.service.BuildJSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@EnableAutoConfiguration
@Import(value = {RestTemplateConfig.class})
public class SpringRestTemplateApp {

    @Autowired
    RestTemplate restTemplate;

    /***********HTTP GET method*************/
    @RequestMapping("")
    //http://localhost:8080 就可以访问
    public String hello(){
        String url = "http://localhost:8080/json";
        JSONObject json = restTemplate.getForEntity(url, JSONObject.class).getBody();
        return json.toJSONString();
    }

    @RequestMapping("/json")
    public Object genJson(){
        JSONObject json = new JSONObject();
        json.put("descp", "this is spring rest template sample");
        return json;
    }

    /**********HTTP POST method**************/
    @RequestMapping("/postApi")
    public Object iAmPostApi(@RequestBody JSONObject parm){
        System.out.println(parm.toJSONString());
        parm.put("result", "hello post");
        return parm;
    }

    @RequestMapping("/post")
    public Object testPost(){
        String url = "http://localhost:8080/postApi";
        JSONObject postData = new JSONObject();
        postData.put("descp", "request for post");
        JSONObject json = restTemplate.postForEntity(url, postData, JSONObject.class).getBody();
        return json.toJSONString();
    }
    //接受浏览器请求，转到其他的服务器上的请求
    @RequestMapping("/testgetCheckCode")
    public Object getCheckCode(){
        String url = "http://localhost:8081/jeecg-boot/sys/getCheckCode";
        JSONObject json = restTemplate.getForEntity(url, JSONObject.class).getBody();
        return json.toJSONString();
    }

    //接受浏览器请求，转到其他的服务器上的请求
    @RequestMapping("/commitCombineidJson")
    public Object commitCombineidJson() throws Exception{
        String url = "http://localhost:8081/jeecg-boot/word/uploadwordpage2list";
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        HttpMethod method = HttpMethod.POST;
        // 以Json的方式提交
       // headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add("Content-Type", "application/json;charset=utf-8");
        //将请求头部和参数合成一个请求
        MultiValueMap<String,String> params= new LinkedMultiValueMap<>();
        BuildJSON buildJSON=new BuildJSON();
        ListWordEntityVO lw= buildJSON.getJson();

        ObjectMapper mapper = new ObjectMapper();
        String content=mapper.writeValueAsString(lw);
        HttpEntity<String> request = new HttpEntity<>(content, headers);
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, request, String.class);

        String body = postForEntity.getBody();

        System.out.println(body);
      /*  HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
        //执行HTTP请求，将返回的结构使用ResultVO类格式化
        ResponseEntity<ResultVO> response = client.exchange(url, method, requestEntity, ResultVO.class);
*/
        return body;
    }
    //接受浏览器请求，转到其他的服务器上的请求
    @RequestMapping("/genword")
    public Object genword() throws Exception{
        String url = "http://localhost:8081/jeecg-boot/word/batchgenerateword2byfilemoduleidandjsonid";
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        HttpMethod method = HttpMethod.POST;
        // 以form的方式提交
         headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //headers.add("Content-Type", "application/json;charset=utf-8");
        //将请求头部和参数合成一个请求
        MultiValueMap<String,String> params= new LinkedMultiValueMap<>();
        params.add("fileid","0f67d720-6e54-4f6a-8a13-dd04541dd009");
        params.add("filemd5","38f42f02dcf13aafd2b0f628ad2569d5");
        params.add("jsonfileid","ce648074-fb40-4396-8096-77cdc07f0c04");
        params.add("combinedid","ce648074-fb40-4396-8096-77cdc07f0c0x");

        BuildJSON buildJSON=new BuildJSON();
        ListWordEntityVO lw= buildJSON.getJson();

      /*  ObjectMapper mapper = new ObjectMapper();
        String content=mapper.writeValueAsString(lw);
        HttpEntity<String> request = new HttpEntity<>(content, headers);
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, request, String.class);

        String body = postForEntity.getBody();

        System.out.println(body);*/
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
        //执行HTTP请求，将返回的结构使用ResultVO类格式化
        ResponseEntity<Result> response = client.exchange(url, method, requestEntity, Result.class);
        return response.getStatusCode();
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringRestTemplateApp.class, args);
    }

}